package com.example.myblog.Service;

import com.example.myblog.entity.User;
import com.example.myblog.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Transactional
    public int register(String username, String password, String name, String gender, int age, String createtime) {
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        user.setName(name);
        user.setGender(gender);
        user.setAge(age);
        user.setDatetime(String.valueOf(new java.util.Date())); // 设置创建时间

        try {
            userRepository.save(user);
            return 1; // 注册成功
        } catch (Exception e) {
            // 处理异常，例如用户名已存在
            return 0; // 注册失败
        }
    }
}